/***************************************************************************
 *   Copyright (C) 2009 by umbacos@gmail.com                               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "udm-common.h"

void * xmalloc ( size_t size );
void unescape_url ( unsigned char *url );

char * escape_hyphens ( char * str, char * ch ) {

	char out[1000];
	char * start;
	char * hyp;
	int i = 0;

	if ( strlen ( str ) > 500 )
		send_message ( "Filename too long please shorten it: ", str );

	memset ( out, 0, 1000 );

	start = str;

	while ( ( hyp = index ( start, ch[0] ) ) != NULL ) {
		strncat ( out, start, hyp - start );
		strcat ( out, "\\" );
		strcat ( out, ch );
		start = hyp + 1;
	}

	strncat ( out, start, strlen ( start ) );

	return strdup ( out );
}

void split_pairs ( unsigned char * qs, char * sep, char * prefix ) {

	char pair[1000];
	char * name;
	unsigned char * value;
	char * token;
#ifdef DEBUG
	char plog[1000];
#endif

	token = strtok ( qs, sep );
	while ( token ) {
		while ( token[0] == ' ' ) token++;
		strncat ( strcpy ( pair, prefix ), token, 990 );
		value = strdup ( index ( pair, '=' ) + 1 );
		unescape_url ( value );
		pair[ index ( pair, '=' ) - pair ] = '\0';
		name = strdup ( pair );
#ifdef DEBUG
		sprintf ( plog, "%s=%s", name, value );
		mylog ( "", plog );
#endif
		setenv ( name, value, 1 );
		token = strtok ( NULL, sep );
	}
}

void get_post_cookie() {

	char * qs;
	char   buf[200];
	long   cl;
	long   cc;
	long   dd;
	int i;

	if ( getenv ( "QUERY_STRING" ) ) {
		qs = strdup ( getenv ( "QUERY_STRING" ) );
		for ( i = 0; qs[i]; i++ ) if ( qs[i] == '+' ) qs[i] = ' ';
#ifdef DEBUG
		mylog ( "QUERY STRING:", qs );
#endif
		split_pairs ( qs, "&", "FORM_" );
	}

	if ( getenv ( "HTTP_COOKIE" ) ) {
		qs = strdup ( getenv ( "HTTP_COOKIE" ) );
#ifdef DEBUG
		mylog ( "COOKIES:", qs );
#endif
		split_pairs ( qs, ";", "FORM_" );
	}

	if ( getenv ( "CONTENT_LENGTH" ) ) {
		if ( cl = strtoul ( getenv ( "CONTENT_LENGTH" ), NULL, 10 ) ) {
			qs = xmalloc ( cl + 2 );
			cc = 0; dd = 0;

			while ( cc < cl ) {
				memset ( &buf, 0, 200 );
				dd = read ( STDIN, buf, 200 );
				strncat ( qs, buf, dd );
				cc += dd;
			}
			qs[cl] = '\0';
#ifdef DEBUG
			mylog ( "CONTENT_LENGTH:", getenv ( "CONTENT_LENGTH" ) );
			mylog ( "POST DATA:", qs );
#endif
			split_pairs ( qs, "&", "FORM_" );
			free ( qs );
		}
	}
}
